import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo4 {
    private static AtomicInteger sum = new AtomicInteger(0);
    private static CountDownLatch countDownLatch = new CountDownLatch(3);

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                sum.getAndIncrement();
            }
            countDownLatch.countDown();
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                sum.getAndIncrement();
            }
            countDownLatch.countDown();
        });

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                sum.getAndIncrement();
            }
            countDownLatch.countDown();
        });

        t1.start();
        t2.start();
        t3.start();

        countDownLatch.await();
        System.out.println("sum = " + sum);
    }
}
